/*File : HeartBeat.java
 * 
 * Fires a heartbeat event regulary.
 *
 */

import InstrumentationPackage.*;
import EventPackage.*;
import java.util.*;

public class HeartBeat extends Thread {
    private EventManagerInterface em = null;// Interface object to the event manager
    private String EvtMgrIP = null;
    boolean Registered = true;
    int ElementID = 0;
    int	Delay = 2500;
    boolean Done=false;

    public HeartBeat(int ElementID)
	{
		// event manager is on the local system
                this.ElementID = ElementID;
		try
		{
			// Here we create an event manager interface object. This assumes
			// that the event manager is on the local machine

			em = new EventManagerInterface();

		}

		catch (Exception e)
		{
			System.out.println("ECSMonitor::Error instantiating event manager interface: " + e);
			Registered = false;

		} // catch

	} //Constructor

	public HeartBeat(int ElementID, String EvmIpAddress )
	{
		// event manager is not on the local system
                this.ElementID = ElementID;
		EvtMgrIP = EvmIpAddress;

		try
		{
			// Here we create an event manager interface object. This assumes
			// that the event manager is NOT on the local machine

			em = new EventManagerInterface( EvtMgrIP );
		}

		catch (Exception e)
		{
			System.out.println("ECSMonitor::Error instantiating event manager interface: " + e);
			Registered = false;

		} // catch

	} // Constructor

        public void run()
	{
            if(em != null)
            {
                while(!Done)
                {
                    //emit a heartbeat
                    Event evt = new Event(100, Integer.toString(ElementID));
                    try{
                    em.SendEvent(evt);
                    Thread.sleep(Delay);
                    }catch(Exception ex)
                    {
                        break;
                    }


                }

            }

        }

        public void Close()
        {
            Done = true;
        }


}
